package com.zfzs.post.exception.handler;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.common.bean.ResponseCode;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.exception.EntityExistException;
import com.zfzs.post.exception.EntityNotFoundException;
import com.zfzs.post.utils.ThrowableUtil;
import com.zfzs.post.common.bean.ResponseCode;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.exception.EntityExistException;
import com.zfzs.post.exception.EntityNotFoundException;
import com.zfzs.post.utils.ThrowableUtil;
import com.zfzs.post.common.bean.ResponseCode;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.utils.ThrowableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ck
 * @date 2018-11-23
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有不可知的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public ResponseResult handleException(Throwable e){
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        Object msg = null;
        msg = "异常信息为:"+e.getMessage();
        return ResponseResult.error(ResponseCode.SERVER_EXCEPTION_ERROR,msg);
    }

    /**
     * 处理 接口无权访问异常AccessDeniedException
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public ResponseResult handleAccessDeniedException(AccessDeniedException e){
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return ResponseResult.error(ResponseCode.NO_PERMISSION_FAIL);
    }

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
	@ExceptionHandler(value = BadRequestException.class)
	public ResponseResult badRequestException(BadRequestException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        Map<String,Object> map = new HashMap<>();
        return ResponseResult.builder().msg(e.getMsg()).code(e.getCode()).data(map).build();
	}

    /**
     * 处理400错误的请求 EntityExist
     * @param e
     * @return  400
     */
    @ExceptionHandler(value = EntityExistException.class)
    public ResponseResult entityExistException(EntityExistException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return ResponseResult.error(ResponseCode.BAD_REQUEST_ERROR);
    }

    /**
     * 处理404 EntityNotFound
     * @param e
     * @return
     */
    @ExceptionHandler(value = EntityNotFoundException.class)
    public ResponseResult entityNotFoundException(EntityNotFoundException e) {
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return ResponseResult.error(ResponseCode.NOT_FOUND_ERROR);
    }

    /**
     * 处理所有接口数据验证异常
     * @param e
     * @returns
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        // 打印堆栈信息
        log.error(ThrowableUtil.getStackTrace(e));
        return ResponseResult.error(ResponseCode.PARAM_FAIL,e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

}
